package com.projects.android.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;

import com.projects.android.Intro;
import com.projects.android.views.Cart;
import com.projects.android.views.ChangePassword;
import com.projects.android.views.CheckoutInfo;
import com.projects.android.views.FoodDetail;
import com.projects.android.views.ForgotPassword;
import com.projects.android.views.ListRestaurant;
import com.projects.android.views.Main;
import com.projects.android.views.ManageOrders;
import com.projects.android.views.Menu;
import com.projects.android.views.Order;
import com.projects.android.views.OrderDetails;
import com.projects.android.views.Register;
import com.projects.android.views.RestaurantFoods;
import com.projects.android.views.UpdateAccount;

public class Utils {
	public static String SERVICE_URI = "";
	public final static DateFormat DATE_FORMATTER = new SimpleDateFormat(
			"MM/dd/yyyy HH:mm:ss");
	public static int SCREEN_WIDTH;
	public static int SCREEN_HEIGHT;

	public static Intent browseIntent(Context ctx, String path) {
		Intent result = new Intent();

		if (path == "Register") {
			result.setClass(ctx, Register.class);
		} else if (path == "Main") {
			result.setClass(ctx, Main.class);
		} else if (path == "Intro") {
			result.setClass(ctx, Intro.class);
		} else if (path == "ListRestaurant") {
			result.setClass(ctx, ListRestaurant.class);
		} else if (path == "Menu") {
			result.setClass(ctx, Menu.class);
		} else if (path == "RestaurantFoods") {
			result.setClass(ctx, RestaurantFoods.class);
		} else if (path == "Cart") {
			result.setClass(ctx, Cart.class);
		} else if (path == "RestaurantInfo") {
			result.setClass(ctx, FoodDetail.class);
		} else if (path == "Order") {
			result.setClass(ctx, Order.class);
		} else if (path == "ManageOrders") {
			result.setClass(ctx, ManageOrders.class);
		} else if (path == "OrderDetails") {
			result.setClass(ctx, OrderDetails.class);
		} else if (path == "ForgotPassword") {
			result.setClass(ctx, ForgotPassword.class);
		} else if (path == "UpdateAccount") {
			result.setClass(ctx, UpdateAccount.class);
		} else if (path == "ChangePassword") {
			result.setClass(ctx, ChangePassword.class);
		} else if (path == "FoodDetail") {
			result.setClass(ctx, FoodDetail.class);
		} else if (path == "CheckoutInfo") {
			result.setClass(ctx, CheckoutInfo.class);
		}

		return result;
	}

	public static AlertDialog showAlertDlg(Context ctx, String title, String msg) {
		return new AlertDialog.Builder(ctx).setTitle(title).setMessage(msg)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {

						/* User clicked OK so do some stuff */
					}
				}).create();
	}

	public static String[] removeElementsAtIndex(String[] input, int index) {
		if (input != null) {
			List<String> list = new ArrayList<String>(Arrays.asList(input));
			for (int i = 0; i < list.size(); i++) {
				if (index == i) {
					list.remove(i);
				}
			}
			return list.toArray(new String[0]);
		} else {
			return new String[0];
		}
	}

	// public final static Comparator<RowData> sDisplayNameComparator = new
	// Comparator<RowData>() {
	// private final Collator collator = Collator.getInstance();
	//
	// public int compare(RowData map1, RowData map2) {
	// return collator.compare(map1.getmTitle(), map2.getmTitle());
	// }
	// };

	public static String getResponse(HttpEntity entity) {
		String response = "";
		try {
			// convert response to string
			response = EntityUtils.toString(entity, HTTP.UTF_8);
		} catch (IOException e) {
			Log.e("getResponse", e.toString());
		}
		// try {
		// InputStream is = null;
		// is = entity.getContent();
		//
		// BufferedReader reader = new BufferedReader(new InputStreamReader(
		// is, "iso-8859-1"), 8);
		// StringBuilder sb = new StringBuilder();
		// String line = null;
		// while ((line = reader.readLine()) != null) {
		// sb.append(line + "\n");
		// }
		// is.close();
		// response = sb.toString();
		// } catch (Exception e) {
		// Log.e("log_tag", "Error converting result " + e.toString());
		// }
		return response;
	}

	public static String buildURL(String url, Map<String, String> mapValues) {
		StringBuilder builder = new StringBuilder();
		builder.append(url);
		int i = 0;
		for (Entry<String, String> pair : mapValues.entrySet()) {
			String key = pair.getKey();
			String value = pair.getValue();
			if (i == 0) {
				builder.append("?");
			}
			builder.append(key).append('=').append(value);
			if (i != mapValues.size() - 1) {
				builder.append('&');
			}
			i++;
		}
		return builder.toString();
	}

	public static byte[] SerializeObject(Object o) {
		byte[] buffer = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos;
		try {
			oos = new ObjectOutputStream(baos);
			oos.writeObject(o);
			oos.close();
			buffer = baos.toByteArray();
		} catch (IOException e) {
			Log.e("SerializeObject", e.toString());
		}
		return buffer;
	}

	// positive value only.
	public static double round(double value, int decimalPlace) {
		double power_of_ten = 1;
		// floating point arithmetic can be very tricky.
		// that's why I introduce a "fudge factor"
		double fudge_factor = 0.05;
		while (decimalPlace-- > 0) {
			power_of_ten *= 10.0d;
			fudge_factor /= 10.0d;
		}
		return Math.round((value + fudge_factor) * power_of_ten) / power_of_ten;
	}
}
